package com.hntx.joymusic.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileToByteUtils {
    static int BUFFER_SIZE = 1024;

    public static byte[] InputStreamTOByte(String file_name, UploadListener listener) throws IOException {
        File file = new File(file_name);
        if (!file.exists()) {
            return null;
        }
        long size = file.length();
        FileInputStream is = new FileInputStream(file_name);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] data = new byte[BUFFER_SIZE];
        int count = -1;
        long trans = 0;
        while ((count = is.read(data, 0, BUFFER_SIZE)) != -1) {
            outStream.write(data, 0, count);
            if (listener != null) {
                trans += count;
                listener.update(trans, size);
            }
        }
        byte[] alldata = outStream.toByteArray();
        return alldata;
    }
}
